package com.ybwh.springboot1.conf;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.ybwh.springboot1.common.ReturnT;

/**
 * 全局异常处理
 *
 * @author Fan Beibei
 * @since 1.0-SNAPSHOT
 */
//@RestControllerAdvice
public class Springboot1ExceptionHandler {

	/**
	 * 处理参数异常
	 *
	 * @return
	 */
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	@ExceptionHandler({ MissingServletRequestParameterException.class })
	public ReturnT<Object> handleMissingServletRequestParameterException(MissingServletRequestParameterException e1) {

		return new ReturnT<Object>(ReturnT.FAIL_CODE, getExceptionMessage(e1), null);
	}

	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	@ExceptionHandler({ TypeMismatchException.class })
	public ReturnT<Object> handleMissingServletRequestParameterException(TypeMismatchException e2) {

		return new ReturnT<Object>(ReturnT.FAIL_CODE, getExceptionMessage(e2), null);
	}

	/**
	 * 处理请求方式不支持异常
	 *
	 * @return
	 */
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public ReturnT<Object> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {

		return new ReturnT<Object>(ReturnT.FAIL_CODE, getExceptionMessage(e), null);
	}

	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	@ExceptionHandler(NoHandlerFoundException.class)
	public ReturnT<Object> handleNoHandlerFoundException(NoHandlerFoundException e) {
		return new ReturnT<Object>(ReturnT.FAIL_CODE, getExceptionMessage(e), null);
	}

	/**
	 * 处理其他异常
	 *
	 * @return
	 */
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	@ExceptionHandler(Throwable.class)
	public ReturnT<Object> handlePlatformException(Throwable e) {
		return new ReturnT<Object>(ReturnT.FAIL_CODE, ExceptionUtils.getStackTrace(e), null);
	}

	/**
	 * 处理文件大小限制异常
	 *
	 * @return
	 */
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	@ExceptionHandler(MultipartException.class)
	public ReturnT<Object> handleMultipartException(MultipartException e) {
		return new ReturnT<Object>(ReturnT.FAIL_CODE, getExceptionMessage(e), null);
	}

	private String getExceptionMessage(Throwable cause) {
		if (cause instanceof MissingServletRequestParameterException) {
			MissingServletRequestParameterException e1 = (MissingServletRequestParameterException) cause;
			return " miss parameters,name=[" + e1.getParameterName() + "],type=[" + e1.getParameterType() + "]";
		}

		if (cause instanceof TypeMismatchException) {
			TypeMismatchException e2 = (TypeMismatchException) cause;
			return "param type  error, required type is[" + e2.getRequiredType() + "],actual value is [" + e2.getValue()
					+ "].\n";
		}

		if (cause instanceof HttpRequestMethodNotSupportedException) {
			HttpRequestMethodNotSupportedException e = (HttpRequestMethodNotSupportedException) cause;
			return "HTTP request type error!! required type is " + e.getSupportedHttpMethods() + ",but your type is "
					+ e.getMethod();
		}
		if (cause instanceof NoHandlerFoundException) {
			NoHandlerFoundException e = (NoHandlerFoundException) cause;
			return "404  url not found!!,request type is " + e.getHttpMethod() + ",url is " + e.getRequestURL();
		}

		if (cause instanceof MultipartException) {
			MultipartException e = (MultipartException) cause;
			return "upload file erorr!! " + ExceptionUtils.getStackTrace(e);
		}

		return ExceptionUtils.getStackTrace(cause);
	}

}
